package vip.meet;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import vip.meet.bean.ConnectionBean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.TimeUnit;


public final class DBConnectionFactory {

    private static final String DB_URL_PATTERN = "jdbc:mysql://%s:%d/%s?useUnicode=true&characterEncoding=utf-8&characterSetResults=utf-8&serverTimezone=Asia/Shanghai";

    private static final String INFORMATION_SCHEMA = "information_schema";

    private static final String DEFAULT_HOST = "127.0.0.1";

    private static final int DEFAULT_PORT = 3306;

    private static final String DEFAULT_USER = "tim";
    private static final String DDL_USER = "root";

    private static final String DEFAULT_PASSWORD = "123456";
    private static final String DDL_PASSWORD = "654321";
    private static final String URL_PROMPT = "url";
    private static final String USERNAME_PROMPT = "username";
    private static final String PASSWORD_PROMPT = "password";

    private static final Cache<String, Map<String, ConnectionBean>> connectionBeanCache = CacheBuilder.newBuilder()
            .maximumSize(5) // 设置缓存的最大容量
            .expireAfterWrite(30, TimeUnit.MINUTES) // 设置缓存在写入一分钟后失效
            .concurrencyLevel(10) // 设置并发级别为10
            .recordStats() // 开启缓存统计
            .build();

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("please add MySQL driver jar");
        }
    }

    public static Connection getInformationSchemaConnection() throws SQLException {
        String url = String.format(DB_URL_PATTERN, DEFAULT_HOST, DEFAULT_PORT, INFORMATION_SCHEMA);
        return DriverManager.getConnection(url, DEFAULT_USER, DEFAULT_PASSWORD);
    }

    public static Connection getConnection(String db) throws SQLException {
        String url = String.format(DB_URL_PATTERN, DEFAULT_HOST, DEFAULT_PORT, db);
        return DriverManager.getConnection(url, DEFAULT_USER, DEFAULT_PASSWORD);
    }

    public static Connection getConnection(String db, String username, String password) throws SQLException {
        String url = String.format(DB_URL_PATTERN, DEFAULT_HOST, DEFAULT_PORT, db);
        return DriverManager.getConnection(url, username, password);
    }

    public static Connection getDDLConnection(String db) throws SQLException {
        String url = String.format(DB_URL_PATTERN, DEFAULT_HOST, DEFAULT_PORT, db);
        return DriverManager.getConnection(url, DDL_USER, DDL_PASSWORD);
    }

    public static Connection getConnection(String host, int port, String db, String user, String password) throws SQLException {
        String url = String.format(DB_URL_PATTERN, host, port, db);
        return DriverManager.getConnection(url, user, password);
    }

    public static String getJDBCUrl(ConnectionBean bean) {
        return String.format(DB_URL_PATTERN, bean.getHost(), bean.getPort(), bean.getDb());
    }

    public static Connection getConnection(ConnectionBean bean) throws Exception {
        return DriverManager.getConnection(getJDBCUrl(bean), bean.getUser(), bean.getPass());
    }


}
